package core

import (
	"github.com/aceld/zinx/ziface"
	//"github.com/aceld/zinx/log"
	log "github.com/cihub/seelog"
	"sync"
)


/*
	设备管理模块
 */

type DevicesManager struct {
	Devices map[string]*Device //当前在线设备
	pLock sync.RWMutex 	//保护devices的互斥读写机制
}

//提供一个对外的世界管理模块句柄
var DevicesMgrObj *DevicesManager




//提供DeviceManager
func init()  {
	DevicesMgrObj = &DevicesManager{
		Devices: make(map[string]*Device),
	}
}



//提供添加一个装置的功能
func (dm *DevicesManager) AddDevice(device *Device)  {
	//将device 添加到装置管理器中
	log.Debug("AddDevice")
	defer log.Debug("AddDevice end")
	dm.pLock.Lock()
	dm.Devices[device.PID] = device
	dm.pLock.Unlock()
}

//从装置信息中移除一个装置
func (dm *DevicesManager) RemoveDeviceByID(pID string)  {
	log.Debug("RemoveDeviceByID")
	defer log.Debug("RemoveDeviceByID end")
	dm.pLock.Lock()
	delete(dm.Devices,pID)
	dm.pLock.Unlock()
}

//通过装置ID 获取对应的装置信息
func (dm *DevicesManager) GetDeviceByPID(pID string) *Device  {
	log.Debug("GetDeviceByPID")
	defer log.Debug("GetDeviceByPID end")
	dm.pLock.RLock()
	defer dm.pLock.RUnlock()
	return dm.Devices[pID]
}

//通过conn 获取对应装置信息
func (dm *DevicesManager) GetDeviceByConn(conn ziface.IConnection) *Device {
	log.Debug("GetDeviceByConn")
	defer log.Debug("GetDeviceByConn end")
	if conn ==nil{
		log.Debug("conn ==nil")
		return nil
	}

	dm.pLock.Lock()
	defer  dm.pLock.Unlock()

	for _,v :=range dm.Devices{
		if v.Conn == conn{
			return dm.Devices[v.PID]
		}
	}
	return nil
}

//通过


//获取所有装置的信息
func (dm *DevicesManager) GetAllDevices() []*Device {
	log.Debug("GetAllDevices")
	dm.pLock.RLock()
	defer dm.pLock.RUnlock()

	//创建返回的device集合切片
	devices := make([]*Device,0)

	//添加切片
	for _,v := range dm.Devices{
		devices = append(devices,v)
	}

	return devices
}

//通过PID设置Conn信息
func (dm *DevicesManager) SetConnByPID(PID string,conn ziface.IConnection ) {
	log.Debug("SetConnByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()
	device := dm.Devices[PID]
	if device !=nil{
		device.Conn = conn
	}
}

//通过PID设置运营商
func (dm *DevicesManager) SetOperatorsByPID(PID string,operators int) {
	log.Debug("SetOperatorsByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()
	device := dm.Devices[PID]
	if device !=nil{
		device.Operators = operators
	}
}

//通过PID设置装置状态
func (dm *DevicesManager) SetStatusByPID(PID string,status int) {

	log.Debug("SetStatusByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()
	device := dm.Devices[PID]
	if device !=nil{
		device.Status = status
	}
}

//通过PID 设置Rssi
func (dm *DevicesManager) SetRssiByPID(PID string,Rssi int){
	log.Debug("SetRssiByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()
	device := dm.Devices[PID]
	if device !=nil{
		device.Rssi = Rssi
	}
}

//通过PID设置 漏电报警标记
func (dm *DevicesManager) SetLeakAlarmFlagByPID(PID string,leakAlarmFlag int){
	log.Debug("SetLeakAlarmFlagByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()
	device := dm.Devices[PID]
	if device !=nil{
		device.LeakAlarmFlag = uint16(leakAlarmFlag)
	}
}

//通过PID设置 温度Ta报警标记
func (dm *DevicesManager) SetTaAlarmFlagByPID(PID string,TaAlarmFlag int){
	log.Debug("SetTaAlarmFlagByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()
	device := dm.Devices[PID]
	if device !=nil{
		device.TaAlarmFlag = uint16(TaAlarmFlag)
	}
}

//通过PID设置 温度Tb报警标记
func (dm *DevicesManager) SetTbAlarmFlagByPID(PID string,TbAlarmFlag int){
	log.Debug("SetTbAlarmFlagByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()

	device := dm.Devices[PID]
	if device !=nil{
		device.TbAlarmFlag = uint16(TbAlarmFlag)
	}
}

//通过PID设置 温度Tc报警标记
func (dm *DevicesManager) SetTcAlarmFlagByPID(PID string,TcAlarmFlag int){
	log.Debug("SetTcAlarmFlagByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()

	device := dm.Devices[PID]
	if device !=nil{
		device.TcAlarmFlag = uint16(TcAlarmFlag)
	}
}

//通过PID设置 温度T0报警标记
func (dm *DevicesManager) SetT0AlarmFlagByPID(PID string,T0AlarmFlag int){
	log.Debug("SetT0AlarmFlagByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()

	device := dm.Devices[PID]

	if device !=nil{
		device.T0AlarmFlag = uint16(T0AlarmFlag)
	}

}

//通过PID设置 过压报警标记
func (dm *DevicesManager) SetOverVoltageByPID(PID string,overVoltageAlarmFlag int){
	log.Debug("SetOverVoltageByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()

	device := dm.Devices[PID]

	if device !=nil{
		device.OverVoltageAlarmFlag  = uint16(overVoltageAlarmFlag)
	}
}

//通过PID设置 欠压报警标记
func (dm *DevicesManager) SetUnderVoltageByPID(PID string,underVoltageAlarmFlag int){
	log.Debug("SetUnderVoltageByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()

	device := dm.Devices[PID]

	if device !=nil{
		device.UnderVoltageAlarmFlag = uint16(underVoltageAlarmFlag)
	}
}

//通过PID设置 过流报警标记
func (dm *DevicesManager) SetOverCurrentByPID(PID string,overCurrentAlarmFlag int){
	log.Debug("SetOverCurrentByPID")
	dm.pLock.Lock()
	defer dm.pLock.Unlock()

	device := dm.Devices[PID]

	if device !=nil{
		device.OverCurrentAlarmFlag = uint16(overCurrentAlarmFlag)
	}
}


